//
//  PublicNoticeVC.m
//  EJLX
//
//  Created by adc on 15/11/17.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "PublicNoticeVC.h"

@interface PublicNoticeVC ()

@end

@implementation PublicNoticeVC
-(void)viewWillAppear:(BOOL)animated{
    [HttpsManager requestForPublicWithType:1 page:0 completionBlock:^(NSMutableArray *publicArray,int totalPage,int currentPage,int pageNum) {
        self.array = publicArray;
        
        if (totalPage == 0) {
            SHOW_ALERT(@"没有最新消息")
        }
        [_tableview reloadData];
    }];
    
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNav];

    [self addTableView];
    [self addTypeBtn];

}
-(void)creatNav{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 30  )];
    title.text = @"系统公告";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(WIDE-50, 7, 30, 30);
    rightBtn.layer.cornerRadius = 20;
    [rightBtn setTitle:@"编辑"  forState:UIControlStateNormal] ;
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12.f];
    [rightBtn addTarget:self action:@selector(editBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:rightBtn];
    
}
-(void)addTableView{
  
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, WIDE, HEIGHT-100) style:UITableViewStylePlain];
    _tableview.delegate =self;
    _tableview.dataSource = self ;
    _tableview.rowHeight = 60;
    _tableview.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableview];

}

-(void)backBtn{
     [self.navigationController popViewControllerAnimated:YES];
}
-(void)editBtn{
    
}
-(void)addTypeBtn{
    for (int i= 0 ; i<2; i++) {
        UIButton *Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        Btn.frame = CGRectMake(WIDE/2.f*i ,64, WIDE/2.f-1, 34);
       // leftBtn.layer.cornerRadius = 20;
        Btn.tag = 103+i;
        Btn.backgroundColor = [UIColor whiteColor];
        [Btn setTitle:(i==0)?@"消息":@"专题" forState:UIControlStateNormal];
        [Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
        [Btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:Btn];
    }
    _smallView = [[UIView alloc] initWithFrame:CGRectMake(10, 98, WIDE/2.f-20, 2)];
    _smallView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:_smallView];
}
-(void)btnClick:(UIButton *)btn{
     if (btn.tag == 103){

            [UIView animateWithDuration:0.1 animations:^{
                self.type = 1;
                _smallView.frame = CGRectMake(10, 98, WIDE/2.f-20, 2);
                
          }];
      NSLog(@"已经加载");
        
    }else{
        self.type = 2;
                [UIView animateWithDuration:0.1 animations:^{
            _smallView.frame = CGRectMake(WIDE/2.f+10, 98, WIDE/2.f-20, 2);
        }];
    }
    
    [HttpsManager requestForPublicWithType:self.type page:0 completionBlock:^(NSMutableArray *publicArray,int totalPage,int currentPage,int pageNum) {
        self.array = publicArray;
        [_tableview reloadData];
    }];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell4"];
    cell = [[NSBundle mainBundle] loadNibNamed:@"UserTableViewCell" owner:nil options:nil][3];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    PublicNotice *notice = [self.array objectAtIndex:indexPath.row];
    self.type = notice.type;
    
    cell.addPublicContentLB.text = notice.title;
    cell.addPublicTimeLB.text =notice.time;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GoToWebView *web = [[GoToWebView alloc] initWithurl:[NSURL URLWithString:[NSString stringWithFormat:@"%@comm/bulletin_detail?bulletinId=%ld",HTTPS_ID,indexPath.row]] title:@"系统公告"];
    [self.navigationController pushViewController:web animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
